# encoding:utf-8

import os
import shutil
import logging
import string
from _helpscript import template_VER_LUA
from _helpscript import template_RELEASE_LUA
from pylibs import LuaPackbin


# 拷贝文件夹下内容
def copy_(frompath_, tarpath_, ignorefile=""):
    '''批量复制文件'''
    for item in os.listdir(frompath_):
        itemobj = os.path.join(frompath_, item)
        if os.path.isfile(itemobj):
            if item != ignorefile:
                shutil.copy(itemobj, tarpath_)
            else:
                logging.info("Copy ignore the file -->>%s", ignorefile)

        elif os.path.isdir(itemobj) and item != "TEMPLATE_SRC_PACK":
            tardir = os.path.join(tarpath_, item)
            if not os.path.isdir(tardir):
                os.mkdir(tardir)
            copy_(itemobj, tardir)


def path_compatible(path):
    '''linux windows文件路径兼容'''
    return string.replace(path, "/", os.sep)


class DefaultParms(object):
    '''默认的脚本配置'''
    _resver = "unknow"
    _feidou_f = "unknow"
    _feidou_gameid = 9999999999
    _cliver = "unknow"
    _binver = "unknow"
    _channel_id = 9999999999
    _os = "unknow"
    _bundlename = "unknow"
    _lang = "unknow"
    DEBUG_LEVEL = 0

    def __init__(self, parmsdic):
        for k, val in parmsdic.items():
            if hasattr(self, k):
                setattr(self, k, val)


class ScriptPack(object):
    '''脚本打包工具'''

    @classmethod
    def excute(cls, frompath, tarpath, toolpath, parmcls, packtype="jitc"):
        '''实际执行脚本'''
        srcdir = frompath
        tmpdir = os.path.join(frompath, "TEMPLATE_SRC_PACK")
        tmpsrcdir = os.path.join(tmpdir, "src")
        if os.path.isdir(tmpdir):
            shutil.rmtree(tmpdir)
        os.mkdir(tmpdir)
        os.mkdir(tmpsrcdir)

        copy_(srcdir, tmpsrcdir)

        # 从模板生成VER.lua / RELEASE.lua
        #=============================
        template_path = path_compatible(toolpath + "/template")
        if isinstance(parmcls, dict):
            parmcls = DefaultParms(parmcls)  # 参数从这里传进去parms dict
        template_VER_LUA(parmcls, template_path,
                         os.path.join(tmpsrcdir, "VER.lua"))
        template_RELEASE_LUA(parmcls, template_path,
                             os.path.join(tmpsrcdir, "RELEASE.lua"))
        #=============================

        import sys
        # sys.exit()
        # pack!
        LuaPackbin.lua_lpack(tmpsrcdir, tarpath, packtype)
        shutil.rmtree(tmpdir)


if __name__ == "__main__":
    print("======================================")
    print("===============LuaZip=================")
    print("======================================")
    confdic = dict(
        _resver = "999",
        _feidou_f = "558821021445", 
        _os = "ios", 
        _lang = "zh_CN",
        _cliver = "v.3",
        _binver = "2.6.9",
        _bundlename = "com.rongyao.yy",
        _feidou_gameid = "5588",
        _channel_id = "11"
    )
    ScriptPack.excute("/data0/public_work/yn/client/trunk/src", "/data0/public_work/yn/client/trunk/src_pak", "/data0/public_work/yn/client/trunk/py_release", confdic, "jitmix")
